package com.isolation.harassed.view;

import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

import com.isolation.harassed.R;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 底部人的导航面板类，充当宿主{@code Activity}的{@code Fragment}实例化工厂并在切换时并通知宿主{@code Activity}
 * 执行后续切换动作 {@code Activity}，任何{@code Activity}希望接收切换通知只需要实现该内部的
 * {@link PageIndexPanel.OnPageIndexChangedListener#OnPageIndexChangedListener
 * OnPageIndexChangedListener} 接口相应函数即可。
 * 
 * @author allon
 * @version 1.0.1
 * @date 2016年3月23日
 */
public class PageIndexPanel extends Fragment implements View.OnClickListener {

	private int highlightIndex;
//	private MapView bmapView = null;
	private String[] titles = null;
	private TitlePanel titlePanel = null;
	private List<View> selectors = new ArrayList<View>();

	@Override
	// This is called after super activity setContentView(int)
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.titles = getResources().getStringArray(R.array.activity_titles);
		this.titlePanel = (TitlePanel) getFragmentManager().findFragmentById(
				R.id.home_title_panel);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.global_pageindex_panel, container,
				false);
	}

	@Override
	@SuppressLint("NewApi")
	// This is called after super activity onCreate(Bundle)
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		this.bmapView = ((MainActivity) getActivity()).bmapView;
		selectors.add(getView().findViewById(R.id.navigation_profile));
		selectors.add(getView().findViewById(R.id.navigation_treat_call));
		selectors.add(getView().findViewById(R.id.navigation_treat_msg));
		selectors.get(0).setOnClickListener(this);
		selectors.get(1).setOnClickListener(this);
		selectors.get(2).setOnClickListener(this);
		titlePanel.setTitle(titles[0]);

		// set default fragment
		selectors.get(0).callOnClick();
	}

	/** 由 index 决定底部按钮游标的高亮位置及顶部标题 */
	private void onChangeIndex(int index) {
		for (int i = 0; i < selectors.size(); i++) {
			View localView = selectors.get(i);
			if (index != i) {
				localView.setSelected(false);
			} else {
				localView.setSelected(true);
				titlePanel.setTitle(titles[i]);
				Fragment[] pages = ((MainActivity) getActivity()).pages;
				if (pages[index] == null) {
					pages[index] = getPageByIndex(index);
				}
			}
		}
	}

	/**
	 * 根据页索引获取页对象
	 * 
	 * @param index
	 *            页索引
	 * @return target page fragment
	 */
	private Fragment getPageByIndex(int index) {
		switch (index) {
		case 0:
			return new ProfilePage();
		case 1:
			return new TreatCallPage();
		case 2:
			return new TreatMsgPage();
		default:
			return null;
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == this.highlightIndex) {
			return;
		}
		this.highlightIndex = v.getId();
		switch (v.getId()) {
		case R.id.navigation_profile:
			onChangeIndex(0);
			/*if (!bmapView.isShown())
				bmapView.setVisibility(View.VISIBLE);*/
			break;
		case R.id.navigation_treat_call:
			onChangeIndex(1);
			/*if (!bmapView.isShown())
				bmapView.setVisibility(View.VISIBLE);*/
			break;
		case R.id.navigation_treat_msg:
			onChangeIndex(2);
			/*if (bmapView.isShown())
				bmapView.setVisibility(View.GONE);*/
			break;
		}
		if (getActivity() instanceof OnPageIndexChangedListener) {
			((OnPageIndexChangedListener) getActivity())
					.onSwitched(this.highlightIndex);
		}
	}

	/**
	 * 定义{@code PageIndexPanel}监听的页面改变事件接口
	 * 
	 * @author allon
	 * @version 1.0.1
	 * @date 2016年3月19日
	 */
	public interface OnPageIndexChangedListener extends EventListener {
		/**
		 * 点击导航条改变页面通知函数
		 * 
		 * @param resId
		 *            被点击按钮的Id
		 */
		public void onSwitched(int resId);
	}
}
